/* Preferences.java
 *
 * Copyright 2011 Aleksey Konovalov
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 * 
 */

package org.jimm.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;
import android.util.Log;

public final class Preferences implements OnSharedPreferenceChangeListener {

    private Context context;

    public static final String P_XSTATUS_INDEX = "xstatusIndex";
    public static final String P_XSTATUS_TITLE = "xstatusTitle";
    public static final String P_XSTATUS_DESCRIPTION = "xstatusDescription";
    public static final String P_PRIVATE_STATUS = "privateStatus";


    final static public int SORT_BY_STATUS = 0;
    final static public int SORT_BY_ONLINE = 1;
    final static public int SORT_BY_NAME = 2;

    public static final String P_AUTO_CONNECT = "autoConnect";
    public static final String P_RECONNECT_NUMBER = "reconnectNumber";
//    public static final String P_CLIENT_ID = "clientId";
    // ����������
    public static final String P_SHOW_APP_ICON = "showAppIcon";
    public static final String P_ALARM_ENABLED = "alarmEnabled";
    // ������ ����
    public static final String P_METACONTACTS_MODE = "metaContactsMode";
    public static final String P_SHOW_AVATARS = "showAvatars";
    public static final String P_SHOW_USER_GROUPS = "showUserGroups";
    public static final String P_HIDE_EMPTY_GROUPS = "hideEmptyGroups";
    public static final String P_SHOW_STATUS_LINE = "showStatusLine";
    public static final String P_HIDE_OFFLINE_CONTACTS = "hideOfflineContacts";
    public static final String P_SORT_UP_WITH_MSG = "sortUpWithMsg";
    public static final String P_CL_SORT_BY = "clSortBy";
    public static final String P_SAVE_TEMP_CONTACT = "saveTempContact";
    // Chat
    public static final String P_SEND_BY_ENTER = "sendByEnter";
    public static final String P_DELIVERY_NOTIFICATION = "deliveryNotification";
    public static final String P_TYPING_MODE = "typingMode";
    // Notifications
    public static final String P_SOUND_ENABLED = "soundEnabled";
    public static final String P_VIBRO_ENABLED = "vibroEnabled";

//    public static final String P_CUT_XSTATUS = "cut_xstatus";
//    public static final String P_SEND_BUTTON_HIDE = "sendButtonHide";
//    public static final String P_SMILE_BUTTON_HIDE = "smileButtonHide";
//    public static final String P_LICENSE = "License";
//    public static final String P_LANG = "lang";
//    public static final String P_SMILE_SPACE = "smileSpace";
//    public static final String P_CAM_ACT = "camAct";

    public boolean antispamEnable;
    public String antispamMsg;
    public String antispamMsgHello;
    public String antispamMsgAnswer;
    public String antispamKeywords;


    public int statusIndex;
    public int xstatusIndex;

    public String[] xstatusTitles = new String[35];
    public String[] xstatusDescriptions = new String[35];

    public int privateStatus;

    public boolean autoConnect;
    public int reconnectNumber;
    public int clientId;

    public boolean showAppIcon;
    public boolean alarmEnabled;

    public boolean metaContactsMode;
    public boolean showAvatars;
    public boolean showUserGroups;
    public boolean hideEmptyGroups;
    public boolean showStatusLine;
    public boolean hideOfflineContacts;
    public int clSortBy;
    public boolean sortUpWithMsg;
    public boolean saveTempContact;

    public boolean sendByEnter;
    public boolean typingMode;
    public boolean deliveryNotification;


    public boolean soundEnabled;
    public boolean vibroEnabled;

    //    public boolean sendButtonHide;
//    public boolean smileButtonHide;
    public boolean license;
    public boolean smileSpaceEnabled;

    public Preferences(Context context) {
        this.context = context;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        preferences.registerOnSharedPreferenceChangeListener(this);
        readPreferences(preferences);
    }

    private void readPreferences(SharedPreferences preferences) {

        //preferences.getBoolean(P_SOUND);
        //preferences.getBoolean(P_VIBRO);

        xstatusIndex = preferences.getInt(P_XSTATUS_INDEX, 0);

        privateStatus = preferences.getInt(P_PRIVATE_STATUS, 0);

        //xstatusTitle = preferences.getString(P_XSTATUS_TITLE, "?");
        //xstatusDescription = preferences.getString(P_XSTATUS_DESCRIPTION, "?");

        for (int i = 0; i < xstatusTitles.length; i++) {
            xstatusTitles[i] = preferences.getString(P_XSTATUS_TITLE + i, "");
        }
        for (int i = 0; i < xstatusDescriptions.length; i++) {
            xstatusDescriptions[i] = preferences.getString(P_XSTATUS_DESCRIPTION + i, "");
        }

        autoConnect = preferences.getBoolean(P_AUTO_CONNECT, false);
        reconnectNumber = Integer.parseInt(preferences.getString(P_RECONNECT_NUMBER, "5"));
        //clientId = preferences.getInt(P_CLIENT_ID, 0);

        // Приложение
        showAppIcon = preferences.getBoolean(P_SHOW_APP_ICON, true);
        alarmEnabled = preferences.getBoolean(P_ALARM_ENABLED, false);
        // Контакт лист
        metaContactsMode = preferences.getBoolean(P_METACONTACTS_MODE, false);
        showAvatars = preferences.getBoolean(P_SHOW_AVATARS, false);
        showUserGroups = preferences.getBoolean(P_SHOW_USER_GROUPS, true);
        hideEmptyGroups = preferences.getBoolean(P_HIDE_EMPTY_GROUPS, true);
        showStatusLine = preferences.getBoolean(P_SHOW_STATUS_LINE, false);
        hideOfflineContacts = preferences.getBoolean(P_HIDE_OFFLINE_CONTACTS, true);
        sortUpWithMsg = preferences.getBoolean(P_SORT_UP_WITH_MSG, false);
        clSortBy = preferences.getInt(P_CL_SORT_BY, 0);
        saveTempContact = preferences.getBoolean(P_SAVE_TEMP_CONTACT, true);
        // Чат
        sendByEnter = preferences.getBoolean(P_SEND_BY_ENTER, false);
        deliveryNotification = preferences.getBoolean(P_DELIVERY_NOTIFICATION, true);
        typingMode = preferences.getBoolean(P_TYPING_MODE, true);

        soundEnabled = preferences.getBoolean(P_SOUND_ENABLED, false);
        vibroEnabled = preferences.getBoolean(P_VIBRO_ENABLED, false);

        antispamEnable = preferences.getBoolean("antispamEnable", false);
        antispamMsg = preferences.getString("antispamMsg", "");
        antispamMsgHello = preferences.getString("antispamMsgHello", "");
        antispamMsgAnswer = preferences.getString("antispamMsgAnswer", "");
        antispamKeywords = preferences.getString("antispamKeywords", "");
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        readPreferences(prefs);
        Log.i("Jimm > savepreferences", "preferences > onSharedPreferenceChanged");
        save();
    }

    public void save() {

        // Загружаем редактор настроек и вписываем новые значения.
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putInt(P_XSTATUS_INDEX, xstatusIndex);
        //editor.putString(P_XSTATUS_TITLE, xstatusTitle);
        //editor.putString(P_XSTATUS_DESCRIPTION, xstatusDescription);
        editor.putInt(P_PRIVATE_STATUS, privateStatus);

        editor.putBoolean(P_HIDE_OFFLINE_CONTACTS, hideOfflineContacts);
        editor.putBoolean(P_SOUND_ENABLED, soundEnabled);
        for (int i = 0; i < xstatusTitles.length; i++) {
            editor.putString(P_XSTATUS_TITLE + i, xstatusTitles[i]);
        }
        for (int i = 0; i < xstatusDescriptions.length; i++) {
            editor.putString(P_XSTATUS_DESCRIPTION + i, xstatusDescriptions[i]);
        }
        try {
            // Сохраняем данные. // Не сохраняются, вызываем  apply вместо  commit, чтобы не было зацикливания
//            editor.commit();
            editor.apply();
        } catch (Throwable t) {
            Log.e("Jimm:Preferences", Log.getStackTraceString(t));
        }
    }
}